package com.fanghuaiming.ml.pay.scheduled;

import com.fanghuaiming.ml.common.model.common.order.MessageListOrderDTO;
import com.fanghuaiming.ml.common.model.enumeration.OrderTransactionEnum;
import com.fanghuaiming.ml.common.model.enumeration.PayStatusEnum;
import com.fanghuaiming.ml.pay.dao.mapper.MlPayMsgMapper;
import com.fanghuaiming.ml.pay.dao.model.MlPayMsg;
import com.fanghuaiming.ml.pay.order.spi.MessageListOrderFeignClient;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/****
 * @description: 定时读取本地处理失败的事务
 * @version:1.0.0
 * @author fanghuaiming
 * @data Created in 2020/12/3 下午6:06
 *
 */
@Component
@Slf4j
public class MessageListDealtransactionsIdScheduled {

    /**
     * 支付微服务
     */
    @Resource
    private MessageListOrderFeignClient messageListOrderFeignClient;

    /**
     * 支付信息表mapper
     */
    @Autowired
    private MlPayMsgMapper mlPayMsgMapper;


    /**
     * @Description: 定时每分钟处理异常失败事务
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/12/3 下午6:22
     * @version:1.0.0
     */
    @Scheduled(cron = "0/10 * * * * ?")
    public void ClearUserCardScheduled() {
        List<MlPayMsg> mlPayMsgList = mlPayMsgMapper.selectUnSendPayMsgList(PayStatusEnum.NO_SEND.getCode());
        if(null == mlPayMsgList || mlPayMsgList.size() == 0){
            log.info("====== 本地支付消息表定时失败事务处理器 暂未发现有未处理支付订单 ======");
            return ;
        }
        log.info("====== 本地支付消息表定时失败事务处理器 检查当前是否有{}条未处理支付订单 ======",mlPayMsgList.size());
        mlPayMsgList.parallelStream().forEach(mlPayMsg -> {
            try {
                MessageListOrderDTO messageListOrderDTO = new MessageListOrderDTO(mlPayMsg.getOrderId());
                OrderTransactionEnum orderTransactionEnum = messageListOrderFeignClient
                        .dealOrder(messageListOrderDTO);
                if(orderTransactionEnum == OrderTransactionEnum.SUCCESS){
                    log.info("======幂等操作 本地支付消息表定时失败事务处理器 调用订单微服务成功,当前处理支付消息:{} ======",mlPayMsg);
                    //发送成功
                    mlPayMsg.setStatus(1);
                    mlPayMsg.setUpdateTime(new Date());
                    //系统更新
                    mlPayMsg.setUpdateUser(0L);
                    //更新支付信息表
                    mlPayMsgMapper.updateByPayId(mlPayMsg);
                }else {
                    log.info("====== 本地支付消息表定时失败事务处理器 调用订单微服务失当前处理支付消息:{} ======",mlPayMsg);
                    mlPayMsg.setFailCount(mlPayMsg.getFailCount() + 1);
                    if(mlPayMsg.getFailCount() > 3){
                        //设置为最终失败,通知报警进行手动补偿
                        mlPayMsg.setStatus(2);
                        log.info("====== 本地支付消息表定时失败事务处理器 调用订单微服务失败超过3次,当前处理支付消息:{},报警通知人工补偿 ======",mlPayMsg);
                    }
                    //系统更新
                    mlPayMsg.setUpdateUser(0L);
                    synchronized (mlPayMsg){
                        mlPayMsg.setUpdateTime(new Date());
                    }
                    //更新支付信息表
                    mlPayMsgMapper.updateByPayId(mlPayMsg);
                }
            } catch (Exception e) {
                log.info("====== 本地支付消息表定时失败事务处理器 调用订单微服务失败,当前处理支付消息:{} ======",mlPayMsg);
                e.printStackTrace();
            }
        });

    }

}
